export var currencyValidator = {
  negative: false,
  format: function (number, negative) {
    if (negative !== undefined) this.negative = negative;
    return this.negative ? (+number).toFixed(2) : Math.abs(+number).toFixed(2);
  },
  parse: function (newString, oldNumber, negative) {
    if (negative !== undefined) this.negative = negative;
    var CleanParse = function (value) {
      return { value: value.toString() }
    }
    var CurrencyWarning = function (warning, value) {
      return { 
        warning: warning,
        value: value,
        attempt: newString 
      }
    }
    var NotAValidDollarAmountWarning = function (value) {
      return new CurrencyWarning(newString + ' is not a valid dollar amount', value)
    }
    var AutomaticConversionWarning = function (value) {
      return new CurrencyWarning(newString + ' was automatically converted to ' + value, value)
    }

    var newNumber = Number(newString)
    var indexOfDot = newString.indexOf('.')
    var indexOfE = newString.indexOf('e')
    var indexOfMinus = newString.indexOf('-')
    if (!this.negative) indexOfMinus = -1
    
    if (isNaN(newNumber)) { 
      if (
        indexOfDot === -1 &&
        indexOfE > 0 &&
        indexOfE === newString.length - 1 &&
        Number(newString.slice(0, indexOfE)) !== 0
      ) {
        return new CleanParse(oldNumber)
      } else if (indexOfMinus === 0 && newString.length === 1) {
        return new CleanParse(newString)
      } else {
        return new NotAValidDollarAmountWarning(oldNumber)
      }
    }
    
    var newCurrencyString = currencyValidator.format(newNumber)
    var newCurrencyNumber = Number(newCurrencyString)

    if (newCurrencyNumber === newNumber) {
      if (indexOfE !== -1 && indexOfE === newString.length - 2) {
        return new AutomaticConversionWarning(newNumber)
      } else {
        return new CleanParse(newNumber)
      }
    } else {
      return new NotAValidDollarAmountWarning(
        newNumber > newCurrencyNumber
          ? newCurrencyNumber
          : oldNumber
      )
    }
  }
}